package com.yntsoft.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.modules.web.Servlets;

import com.github.pagehelper.PageInfo;
import com.yntsoft.entity.Permission;
import com.yntsoft.service.PermissionService;
import com.yntsoft.service.UserService;
import com.yntsoft.util.Constant;
import com.yntsoft.util.ResponCode;

/**
 * 用户管理控制层
 * @author Tony
 *
 */
@Controller
@RequestMapping("permission")
public class PermissionManagerController {

	@Autowired
	private PermissionService permissionService;
	
	/**
	 * 用户列表分页查询
	 * @param request
	 * @param model
	 * @return  
	 */
	@RequestMapping(value="permissionPage")
	@RequiresPermissions("permission:list")
	public String permissionPage(HttpServletRequest request,Model model){
		Map<String, Object> searchParam = Servlets.getParametersStartingWith(request,"search_");
		PageInfo<Permission> Permissions = permissionService.queryPermissionList(searchParam,Constant.pageSize);
		model.addAttribute("pageInfo", Permissions);
		return "permission/permissionPage";
	}
	
	
	/**
	 * 添加或者修改用户信息
	 * @param request
	 * @param Permission
	 * @param attributes  
	 * @return   
	 */
	@RequestMapping(value="addPermission" ,method=RequestMethod.POST)
	@RequiresPermissions("permission:addPermission")
	public String savePermission(HttpServletRequest request,Permission permission,RedirectAttributes redirectAttributes){
		Integer rst = permissionService.savePermission(permission);
		if(rst != null && rst != 0){//修改
			redirectAttributes.addFlashAttribute("message", "操作成功！");
		}else{//添加
			redirectAttributes.addFlashAttribute("message", "操作失败！");
		} 
		return "redirect:/permission/permissionPage.do";
	}
	
	/**
	 * 获取用户信息
	 * @param request
	 * @param Permission
	 * @param attributes
	 * @return   
	 */
	@RequestMapping("getPermissionInfo")
	@ResponseBody
	public Map<String, Object> getPermissionInfo(HttpServletRequest request,Integer permissionId,Model model){
		Map<String, Object> map = new HashMap<String,Object>();
		Permission rst = permissionService.getPermissionById(permissionId);
		if(rst != null){
			map.put("code", ResponCode.code_success);
			map.put("permission", rst);
		}else{
			map.put("permission", ResponCode.code_fail);
		}
		return map;
	}


	/**
	 * 获取用户信息
	 * @param request
	 * @param Permission
	 * @param attributes 
	 * @return    permission:permissionDel
	 */
	@RequestMapping("permissionDel")
	@RequiresPermissions("permission:permissionDel")
	public String permissionDel(HttpServletRequest request,RedirectAttributes redirectAttributes,String ids){
		//判断选择的权限是否已经有角色拥有
		if (ids != null && ids!="") {
			Integer rst = permissionService.permissionDel(ids);
			if(rst != null && rst != 0){
				redirectAttributes.addFlashAttribute("message", "删除成功");
			}else{
				redirectAttributes.addFlashAttribute("message", "删除失败");
			}
		}else{
			redirectAttributes.addFlashAttribute("message", "删除失败");
		}
		return "redirect:/permission/permissionPage.do";
	}
	
	
	/**
	 * 菜单列表
	 * @param request
	 * @param Permission
	 * @param attributes
	 * @return  
	 */
	@RequestMapping("queryPermissionMenu")
	@ResponseBody
	public Map<String, Object> queryPermissionMenu(String type){
		Map<String,Object> map= new HashMap<>();
		List<Permission> menus = permissionService.queryPermissionMenu(type);
		map.put("menus", menus);
 		return map;
	}
	
}
